package cn.fudan.pms.service.impl;

import cn.fudan.pms.domain.po.Issue;
import cn.fudan.pms.domain.po.IssueExample;
import cn.fudan.pms.domain.po.IssueRecord;
import cn.fudan.pms.mbg.mapper.IssueMapper;
import cn.fudan.pms.mbg.mapper.IssueRecordMapper;
import cn.fudan.pms.service.IssueService;
import cn.fudan.pms.util.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class IssueServiceImpl implements IssueService {
    @Autowired
    private IssueMapper issueMapper;
    @Autowired
    private IssueRecordMapper issueRecordMapper;

    @Override
    public String addIssue(String jobUuid, String category, String residentUuid, String residentName, String content) {
        Issue issue = new Issue();
        issue.setUuid(UUIDUtil.generateUUID(category));
        issue.setIssueTime(new Date());
        issue.setCategory(category);
        issue.setResidentUuid(residentUuid);
        issue.setResidentName(residentName);
        issue.setContent(content);
        issue.setStatus("open");
        issueMapper.insert(issue);
        return issue.getUuid();
    }

    @Override
    public int addIssueRecord(String issueUuid, String operatorUuid, String content) {
        IssueExample issueExample = new IssueExample();
        issueExample.createCriteria().andUuidEqualTo(issueUuid);
        Issue issue = issueMapper.selectByExample(issueExample).get(0);
        IssueRecord issueRecord = new IssueRecord();
        issueRecord.setIssueUuid(issueUuid);
        issueRecord.setContent(content);
        issueRecord.setRecordTime(new Date());
        issueRecord.setResidentUuid(issue.getResidentUuid());
        issueRecord.setOperatorUuid(operatorUuid);
        issueRecordMapper.insert(issueRecord);
        return issueRecord.getId();
    }

    @Override
    public void done(String issueUuid) {
        IssueExample issueExample = new IssueExample();
        issueExample.createCriteria().andUuidEqualTo(issueUuid);
        Issue issue = issueMapper.selectByExample(issueExample).get(0);
        issue.setStatus("close");
        issueMapper.updateByExampleSelective(issue, issueExample);
    }
}
